package com.pearadmin.hospital.controller;

import com.github.pagehelper.PageInfo;
import com.pearadmin.hospital.domain.HospitalBillinfo;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.common.tools.secure.SecurityUtil;
import com.pearadmin.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.pearadmin.hospital.service.IHospitalBillinfoService;

import java.time.LocalDateTime;

/**
 * 账单信息Controller
 *
 * @author yuan
 * @date 2021-07-29
 */
@RestController
@RequestMapping("/hospital/billinfo")
public class HospitalBillinfoController extends BaseController
{
    private String prefix = "hospital/billinfo";

    @Autowired
    private IHospitalBillinfoService hospitalBillinfoService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/hospital/billinfo/main','hospital:billinfo:main')")
    public ModelAndView main()
    {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询账单信息列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/hospital/billinfo/data','hospital:billinfo:data')")
    public ResultTable list(@ModelAttribute HospitalBillinfo hospitalBillinfo, PageDomain pageDomain)
    {
        PageInfo<HospitalBillinfo> pageInfo = hospitalBillinfoService.selectHospitalBillinfoPage(hospitalBillinfo,pageDomain);
        return pageTable(pageInfo.getList(),pageInfo.getTotal());
    }

    /**
     * 新增账单信息
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/hospital/billinfo/add','hospital:billinfo:add')")
    public ModelAndView add()
    {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存账单信息
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/hospital/billinfo/add','hospital:billinfo:add')")
    public Result save(@RequestBody HospitalBillinfo hospitalBillinfo)
    {
        SysUser sysUser = (SysUser)SecurityUtil.currentUserObj();
        hospitalBillinfo.setCreateTime(LocalDateTime.now());
        hospitalBillinfo.setCreateBy(sysUser.getUserId());
        hospitalBillinfo.setCreateName(sysUser.getUsername());
        return decide(hospitalBillinfoService.insertHospitalBillinfo(hospitalBillinfo));
    }

    /**
     * 修改账单信息
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/hospital/billinfo/edit','hospital:billinfo:edit')")
    public ModelAndView edit(Integer id, ModelMap mmap)
    {
        HospitalBillinfo hospitalBillinfo = hospitalBillinfoService.selectHospitalBillinfoById(id);
        mmap.put("hospitalBillinfo", hospitalBillinfo);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存账单信息
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/hospital/billinfo/edit','hospital:billinfo:edit')")
    public Result update(@RequestBody HospitalBillinfo hospitalBillinfo)
    {
        SysUser sysUser = (SysUser)SecurityUtil.currentUserObj();
        hospitalBillinfo.setUpdateTime(LocalDateTime.now());
        hospitalBillinfo.setUpdateBy(sysUser.getUserId());
        hospitalBillinfo.setUpdateName(sysUser.getUsername());
        return decide(hospitalBillinfoService.updateHospitalBillinfo(hospitalBillinfo));
    }

    /**
     * 删除账单信息
     */
    @ResponseBody
    @DeleteMapping( "/batchRemove")
    @PreAuthorize("hasPermission('/hospital/billinfo/remove','hospital:billinfo:remove')")
    public Result batchRemove(String ids)
    {
        return decide(hospitalBillinfoService.deleteHospitalBillinfoByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/hospital/billinfo/remove','hospital:billinfo:remove')")
    public Result remove(@PathVariable("id") Integer id)
    {
        return decide(hospitalBillinfoService.deleteHospitalBillinfoById(id));
    }
}
